package com.users.web;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;
import com.users.bean.Users;
import com.users.service.UsersService;
import com.users.util.BaseAction;
import com.users.util.PaginationSupport;

public class UsersAction extends BaseAction{
	private static final long serialVersionUID = 1L;

	private UsersService usersService;
	private Users users;
	private String inputRand;
	private String msg;
	private String keyValue;
	private int pageNum;
	private List<String> jsonList;
	private PaginationSupport pagination;
	private String userJson;

	// 用户登录
	public String userLogin() {
		if (users == null) {
			users = new Users();
		}
		users = usersService.getUsersByUserNameAndPassWord(users.getUserName(), users.getPassWord());
		if (users != null) {
			HttpSession session = super.getRequest().getSession();
			session.setAttribute("user", users);
			return SUCCESS;
		}
		msg = "登录失败，用户名或密码错误！";
		return INPUT;
	}
	// 用户注册
	public String userReg() {
		String sysRand = (String) getRequest().getSession().getAttribute("random");
		if (sysRand.equalsIgnoreCase(inputRand)) {
			if (usersService.addUsers(users) > 0) {
				HttpSession session = super.getRequest().getSession();
				session.setAttribute("user", users);
				return SUCCESS;
			}
		}
		msg = "验证码输入错误！";
		return INPUT;
	}

	public String getAll() throws Exception {
		String page = getRequest().getParameter("page");
		if (this.keyValue != null) {
			this.keyValue = new String(this.keyValue.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (page != null) {
			int p = Integer.parseInt(page);
			pagination.setStartIndex(((p - 1) * pagination.getPageSize()));// p总是从1开始，但记录从0开始，所以p-1
		}
		this.pagination = usersService.queryUsersByPage(pagination.getPageSize(), pagination.getStartIndex(), users);
		getRequest().setAttribute("page", pagination.page(pagination.getPages(), pagination.getCurrentPage()));
		getRequest().setAttribute("keys", keyValue);
		return SUCCESS;
	}

	public String auto() throws Exception {
		if (users == null) {
			users = new Users();
		}
		jsonList = usersService.queryUserName(users.getUserName());
		return SUCCESS;
	}
	public String queryAllUsers() throws Exception {
		List<Users> userliList = usersService.queryUserList();
		StringBuilder jsonData = new StringBuilder();
		jsonData.append("[");
		if (userliList != null && userliList.size() > 0) {
			for (Users users : userliList) {
				jsonData.append("{ 'userName':'" + users.getUserName() + "', 'passWord':'" + users.getPassWord() + "'}");
				jsonData.append(",");
			}
			jsonData.setCharAt(jsonData.length() - 1, ']');
		} else {
			jsonData.append("]");
		}
		userJson = jsonData.toString();
//		PrintWriter out = null;
//		// 方案I：把输出的json字符串/xml写入输出流，让页面直接获得数据，return null
//		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
//		out = ServletActionContext.getResponse().getWriter();
//		out.print(userJson); // treeNodeJson就是拼装的json或xml
//		out.close();
		return SUCCESS;
	}

	@JSON(serialize = false)
	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}
	@JSON(serialize = false)
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	public void setUsersService(UsersService usersService) {
		this.usersService = usersService;
	}
	@JSON(serialize = false)
	public String getInputRand() {
		return inputRand;
	}
	public void setInputRand(String inputRand) {
		this.inputRand = inputRand;
	}
	@JSON(serialize = false)
	public String getKeyValue() {
		return keyValue;
	}
	public void setKeyValue(String keyValue) {
		this.keyValue = keyValue;
	}
	@JSON(serialize = false)
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	@JSON(serialize = false)
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	@JSON(serialize = true)
	public List<String> getJsonList() {
		return jsonList;
	}
	public void setJsonList(List<String> jsonList) {
		this.jsonList = jsonList;
	}
	@JSON(serialize = false)
	public String getUserJson() {
		return userJson;
	}
	public void setUserJson(String userJson) {
		this.userJson = userJson;
	}
}
